package com.aier.service.inventory;


import cn.hutool.core.date.DateUtil;
import com.aier.bean.inventory.InventoryBean;
import com.aier.bean.inventory.InventoryRecordsBean;
import com.aier.bean.inventory.ProductEntryBean;
import com.aier.bean.common.GlobalCode;
import com.aier.bean.common.OperateType;
import com.aier.exception.CastException;
import com.aier.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;

@Service
public class InventoryRecordsService {

    @Resource
    private InventoryService inventoryService;

    @Resource
    private DateUtils dateUtils;

    @Resource
    private ProductEntryService productEntryService;


    /**
     * 构建商品信息表
     */
    public Map<String, List<InventoryRecordsBean>> buildInventoryRecordsTable(){
        Map<String, List<InventoryRecordsBean>> result = new HashMap<>();
        try {
            List<ProductEntryBean> productEntryBeanList = productEntryService.getAllData();
            LocalDate startDate = productEntryBeanList.get(0).getEnterDate();
            LocalDate endDate = productEntryBeanList.get(productEntryBeanList.size()-1).getEnterDate();
            /** 录入信息所有的年月列表 **/
            List<String> yearAndMonthList = dateUtils.getAllYearAndMonth(startDate, endDate);
            /** 获取所有的库存信息 **/
            List<InventoryBean> inventoryBeanList = inventoryService.getAllInventoryInfo();
            for (String yearAndMonth : yearAndMonthList) {
                List<InventoryRecordsBean> inventoryRecordsBeanList = new ArrayList<>();
                for (InventoryBean inventoryBean : inventoryBeanList) {
                    /** 记录当前月份的入库汇总 **/
                    int inInventorySum = 0;
                    /** 记录当前月份的出库汇总 **/
                    int outInventorySum = 0;
                    String modelName = inventoryBean.getModelName();
                    String salesperson = inventoryBean.getSalesperson();
                    String sku = inventoryBean.getSku();
                    InventoryRecordsBean inventoryRecordsBean = new InventoryRecordsBean();
                    LocalDate localDate = dateUtils.getLocalDateByString(yearAndMonth + "01", "yyyyMMdd");
                    LocalDate firstDay = dateUtils.getDateFirstDayOfMonth(localDate);
                    LocalDate lastDay = dateUtils.getDateLastDayOfMonth(localDate);
                    inventoryRecordsBean.setModelName(modelName);
                    inventoryRecordsBean.setSalesperson(salesperson);
                    inventoryRecordsBean.setSku(sku);
                    List<ProductEntryBean> recordList = productEntryService.getProductEntryByModelNameAndDate(modelName, firstDay, lastDay);
                    for (ProductEntryBean record : recordList) {
                        generateInventoryRecordsBeanByProductEntry(inventoryRecordsBean, record);
                        if (record.getOperateType().equals(OperateType.inInventory)){
                            inInventorySum = inInventorySum + record.getEnterQuantity();
                            inventoryBean.setInventoryQuantity(inventoryBean.getInventoryQuantity() + record.getEnterQuantity());
                        }
                        if (record.getOperateType().equals(OperateType.outInventory)){
                            outInventorySum = outInventorySum + record.getEnterQuantity();
                            inventoryBean.setInventoryQuantity(inventoryBean.getInventoryQuantity() - record.getEnterQuantity());
                        }
                    }
                    inventoryRecordsBean.setInInventory(inInventorySum);
                    inventoryRecordsBean.setOutInventory(outInventorySum);
                    inventoryRecordsBean.setCurrentInventory(inventoryBean.getInventoryQuantity());
                    inventoryRecordsBeanList.add(inventoryRecordsBean);
                }
                result.put(yearAndMonth, inventoryRecordsBeanList);
            }
        }catch (Exception e){
            e.printStackTrace();
            CastException.cast(GlobalCode.BUILD_TABLE_FAIL);
        }
        return result;
    }

    /**
     * 将商品录入信息记录到商品记录Bean中
     * @param productEntryBean
     * @return
     */
    public InventoryRecordsBean generateInventoryRecordsBeanByProductEntry(InventoryRecordsBean inventoryRecordsBean,ProductEntryBean productEntryBean){
        LocalDate enterDate = productEntryBean.getEnterDate();
        int dayOfMonth = 0;
        if (enterDate == null){
            dayOfMonth = DateUtil.date().dayOfMonth();
        }else {
            dayOfMonth = enterDate.getDayOfMonth();
        }
        switch (dayOfMonth){
            case 1:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setOneIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setOneOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 2:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTwoIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTwoOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 3:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setThreeIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setThreeOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 4:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setFourIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setFourOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 5:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setFiveIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setFiveOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 6:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setSixIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setSixOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 7:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setSevenIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setSevenOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 8:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setEightIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setEightOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 9:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setNineIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setNineOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 10:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTenIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTenOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 11:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setElevenIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setElevenOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 12:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTwelveIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTwelveOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 13:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setThirteenIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setThirteenOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 14:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setFourteenIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setFourteenOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 15:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setFifteenIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setFifteenOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 16:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setSixteenIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setSixteenOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 17:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setSeventeenIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setSeventeenOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 18:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setEighteenIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setEighteenOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 19:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setNineteenIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setNineteenOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 20:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTwentyIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTwentyOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 21:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTwentyOneIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTwentyOneOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 22:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTwentyTwoIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTwentyTwoOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 23:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTwentyThreeIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTwentyThreeOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 24:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTwentyFourIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTwentyFourOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 25:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTwentyFiveIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTwentyFiveOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 26:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTwentySixIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTwentySixOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 27:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTwentySevenIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTwentySevenOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 28:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTwentyEightIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTwentyEightOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 29:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setTwentyNineIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setTwentyNineOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 30:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setThirtyIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setThirtyOut(productEntryBean.getEnterQuantity());
                }
                break;
            case 31:
                if (productEntryBean.getOperateType().equals(OperateType.inInventory)){
                    inventoryRecordsBean.setThirtyOneIn(productEntryBean.getEnterQuantity());
                }else if (productEntryBean.getOperateType().equals(OperateType.outInventory)){
                    inventoryRecordsBean.setThirtyOneOut(productEntryBean.getEnterQuantity());
                }
                break;
        }
        return inventoryRecordsBean;
    }
}
